package com.example.list;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.util.Log;

import java.util.Stack;

public class ActivityStackManager {

    @SuppressLint("StaticFieldLeak")
    private static ActivityStackManager INSTANCE;

    public static ActivityStackManager getInstance() {
        if (null == INSTANCE) {
            INSTANCE = new ActivityStackManager();
        }
        return INSTANCE;
    }

    private ActivityStackManager() {
    }

    /**
     * 最顶部的activity
     */
    public Activity topActivity;
    /**
     * 当前Activity个数
     */
    public int activityCount = 0;

    public final Stack<Activity> activityStack = new Stack<Activity>();

    public void init(Application application) {
        application.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                addActivityToStack(activity);
                Log.d("activityName", "onActivityCreated: "+activity.getClass().getSimpleName());
            }

            @Override
            public void onActivityStarted(Activity activity) {
                activityCount++;
            }

            @Override
            public void onActivityResumed(Activity activity) {
                topActivity = activity;
            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {
                activityCount--;
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {
                popActivityToStack(activity);
            }
        });
    }


    /**
     * 获取顶部activity
     */
    public Activity getTopActivity() {
        return topActivity;
    }

    /**
     * APP是否在后台
     */
    public boolean isBackGround() {
        return activityCount == 0;
    }

    /**
     * 添加 Activity 到管理栈
     *
     * @param activity Activity
     */
    public void addActivityToStack(Activity activity) {
        activityStack.push(activity);
    }

    /**
     * 弹出栈内指定Activity 不finish
     *
     * @param activity Activity
     */
    public void popActivityToStack(Activity activity) {
        if (!activityStack.empty()) {
            for (Activity element : activityStack) {
                if (element == activity) {
                    Log.d("activityName", "popActivityToStack: "+activity.getClass().getSimpleName());
                    activityStack.remove(activity);
                    return;
                }
            }
        }
    }

    /**
     * 返回到上一个 Activity 并结束当前 Activity
     */
    public void backToPreviousActivity() {
        if (!activityStack.empty()) {
            Activity activity = activityStack.pop();
            if (!activity.isFinishing()) {
                activity.finish();
            }
        }
    }

    /**
     * 根据类名 判断是否是当前的 Activity
     *
     * @param cls Class<*> 类名
     * @return Boolean
     */
    public boolean isCurrentActivity(Class<Activity> cls) {
        Activity currentActivity = getCurrentActivity();
        if (currentActivity != null) {
            return currentActivity.getClass() == cls;
        }
        return false;
    }

    /**
     * 获取当前的 Activity
     */
    public Activity getCurrentActivity() {
        if (activityStack.size() > 0) {
            return activityStack.lastElement();
        }
        return null;
    }

    /**
     * 结束一个栈内指定类名的 Activity
     *
     * @param cls Class<*>
     */
    public void finishActivity(Class<Activity> cls) {
        for (Activity activity : activityStack) {
            if (activity.getClass() == cls) {
                if (!activity.isFinishing()) {
                    activity.finish();
                    return;
                }
            }
        }
    }

    /**
     * 弹出其他 Activity
     */
    public void popOtherActivity() {
        Activity current = getCurrentActivity();
        if (current != null) {
            for (Activity activity : activityStack) {
                if (current != activity) {
                    activityStack.remove(activity);
                    activity.finish();
                }
            }
        }
    }

    /**
     * 返回到指定 Activity
     */
    public void backToSpecifyActivity(Class<Activity> activityClass) {
        // 获取栈最上面的Activity
        Activity lastElement = activityStack.lastElement();
        for (Activity activity : activityStack) {
            if (activity.getClass() == activityClass) {
                if (lastElement.getClass() == activityClass) {
                    return;
                } else {
                    activityStack.remove(lastElement);
                    lastElement.finish();
                }
            }
        }
    }

    /**
     * 退出所有activity
     */
    void exit() {
        for (Activity activity : activityStack) {
            activityStack.remove(activity);
            Log.d("activityName", "exit: "+activity.getClass().getSimpleName());
            if (!activity.isFinishing()) {
                activity.finish();
            }
        }
    }

}

